package cn.wolfcode.crm.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wolfcode.crm.domain.RechargeBill;
import cn.wolfcode.crm.mapper.MemberMapper;
import cn.wolfcode.crm.mapper.RechargeBillMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRechargeBillService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PageUtils;

@Service
public class RechargeServiceImpl implements IRechargeBillService{
    @Autowired
    private RechargeBillMapper mapper;
    @Autowired
    private MemberMapper memberMapper;

    public void save(RechargeBill entity) {
        //计算充值后金额,设置
        //看是充值还是扣费
        if(entity.getRekind()==1){
            //充值
            entity.setAmountafter(entity.getAmountbefore().add(entity.getGiftamount()
                    .add(entity.getReamount())));
        }else{
            //扣费
            entity.setReamount(entity.getReamount().multiply(new BigDecimal("-1")));
            entity.setAmountafter(entity.getAmountbefore().add(entity.getGiftamount()
                    .add(entity.getReamount())));
        }
        //设置充值时间
        entity.setTime(new Date());

        // TODO: 2018/7/12 设置充值的操作员工,做登录校验之后检查
        entity.setOperator("黄懂平");
        /*Employee operator = (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setOperator("");*/
        mapper.insert(entity);

        //充值后更改会员卡内余额
        memberMapper.changeBalance(entity.getMember().getId(),entity.getAmountafter());


    }

    public PageResult query(QueryObject qo) {
		return PageUtils.query(qo, mapper);
	}







	//以下方法不会使用,未实现
    public void update(RechargeBill entity) {

    }

    public void delete(Long id) {

    }

    public List<RechargeBill> list() {
        return null;
    }

    public RechargeBill get(Long id) {
        return null;
    }


}
